﻿using Newtonsoft.Json;
using PLM.ConfigurationCenter.Core.Expansion;
using PLM.ConfigurationCenter.Handler;
using PLM.ConfigurationCenter.Model;
using PLM.ConfigurationCenter.Service;
using PLM.ConfigurationCenter.Service.Handler;
using PLM.ConfigurationCenter.SqlSugar;
using Sunny.UI;
using System;
using System.IO;
using System.Windows.Forms;

namespace PLM.ConfigurationCenter {
    public partial class ServerSettingForm : UIForm {


        public ServerModel Model { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serverCard"></param>
        public ServerSettingForm(ServerModel serverCard) {
            InitializeComponent();
            Model = serverCard;
            BindData();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindData() {
            this.tb_serverid.Text = Model.Id;
            this.tb_serverip.Text = Model.IpAddress;
            this.tb_servername.Text = Model.SiteName;
            this.tb_serverport.Text = Model.SitePort.ToString();
            this.uiLinkLabel1.Text = $"{this.tb_serverip.Text}:{this.tb_serverport.Text}";
            if (!Model.SiteName.IsEmpty()) {
                this.tb_serverpath.Text = ServerHandler.GetSitePath(Model.Id, Model.SiteName);
            }
        }
        /// <summary>
        /// 值变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tb_name_TextChanged(object sender, EventArgs e) {
            this.tb_serverpath.Text = ServerHandler.GetSitePath(Model.Id, this.tb_servername.Text);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Default_Click(object sender, EventArgs e) {
            ServerHandler.FillDefaultServer(Model);
            BindData();
        }
        /// <summary>
        /// 保存
        /// </summary>
        private void btn_Save_Click(object sender, EventArgs e) {
            var res = CheckInputData();
            if (!res.IsEmpty()) {
                MessageBox.Show(res);
                return;
            }
            Model.SiteName = this.tb_servername.Text;
            Model.SitePort = this.tb_serverport.Text.ToIntOrNull();
            Model.IpAddress = this.tb_serverip.Text;
            Model.Status = Model.Status == 0 ? 1 : Model.Status;
            DbManager.DbContext.Updateable(Model).ExecuteCommand();
            //创建文件夹
            var dicpath = ServerHandler.GetSitePath(Model.Id, Model.SiteName);
            if (!Directory.Exists(dicpath)) {
                Directory.CreateDirectory(dicpath);
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string CheckInputData() {
            var name = this.tb_servername.Text.Trim();
            if (name.IsEmpty()) {
                return "没有输入网站名称！";
            }
            var ip = this.tb_serverip.Text.Trim();
            if (ip.IsEmpty()) {
                return "没有输入服务地址！";
            }
            var port = this.tb_serverport.Text.Trim();
            if (port.IsEmpty()) {
                return "没有输入服务端口号！";
            }
            return "";
        }
    }
}
